// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App'
import router from './router'
import VueResource from 'vue-resource'
import Vuex from 'vuex'
Vue.use(Vuex)
import { InfiniteScroll, DatetimePicker } from 'mint-ui'
Vue.use(InfiniteScroll);
Vue.component(DatetimePicker.name, DatetimePicker);
import 'mint-ui/lib/style.css'
Vue.prototype.HOST = '/geocoder'

import {
    DatetimePlugin,
    ConfirmPlugin,
    ToastPlugin,
    LoadingPlugin,
    Tabbar,
    TabbarItem,
    Checklist,
    XSwitch,
    XAddress,
    Popup,
    TransferDom,
    XDialog,
    TabItem,
    Tab,
    SwiperItem,
    Swiper,
    Datetime,
    Toast,
    Scroller,
    LoadMore,
    DatetimeView,
    AlertPlugin
} from 'vux'
require("./components/common/common.less");
import CONSTANT from "../wechat/static/constant/urlconstant";
import common from "../wechat/static/common/common";
Vue.prototype.$common = common
import store from "./store";
Vue.prototype.$store = store
Vue.prototype.$CONSTANT = CONSTANT
import { XHeader } from 'vux'
import { DatetimeRange } from 'vux'
import 'lib-flexible/flexible'
import VueClipboard from 'vue-clipboard2'
import VConsole from 'vconsole/dist/vconsole.min.js' //import vconsole
if ((location.href.includes('icebartech') || location.href.includes('http://localhost')) || location.href.includes('fyf')) {
    let vConsole = new VConsole() // 初始化
}
Vue.use(VueClipboard)
Vue.component('x-header', XHeader)
Vue.use(require('vue-wechat-title'))
Vue.use(VueResource)
Vue.use(LoadingPlugin)
Vue.use(DatetimePlugin)
Vue.use(ConfirmPlugin)
Vue.use(ToastPlugin)
Vue.use(Tabbar)
Vue.use(TabbarItem)
Vue.use(Checklist)
Vue.use(XSwitch)
Vue.use(XAddress)
Vue.use(Popup)
Vue.use(XDialog)
Vue.use(TabItem)
Vue.use(Tab)
Vue.use(SwiperItem)
Vue.use(Datetime)
Vue.use(Toast)
Vue.use(Scroller)
Vue.use(LoadMore)
Vue.use(DatetimeView)
Vue.use(AlertPlugin)
Vue.directive('transfer-dom', TransferDom)

Vue.component('swiper', Swiper)
Vue.component('datetime-range', DatetimeRange)
Vue.config.productionTip = false
const APPID = "wx1a6379b897780c7b";
import "../wechat/static/common/wechat-jsdk";
let webUrl = decodeURIComponent(window.location.href),path;
if (webUrl.includes('phone')&&!webUrl.includes('code=')) {
    localStorage.setItem('userType', 'normal')
} else if (webUrl.includes('password')&&!webUrl.includes('code=')) {
    if (webUrl.includes('?type=store')) {
        localStorage.setItem('userType', 'store')
    } else {
        localStorage.setItem('userType', 'special')
    }
}


const API = (webUrl.includes('icebartech') || webUrl.includes('http://localhost')) ?
    'https://enjoyshop.icebartech.com/h5' :
    'https://www.lexiangfuli.vip/wechat/'

function intercept() {
    // router.beforeEach((to, from, next) => {
    if (!localStorage.getItem("weixinId") || localStorage.getItem("weixinId") == 'null') {        
        if (webUrl.indexOf("code=") > 0) {
            let code = webUrl.split("code=")[1].split("&")[0];
            let url = `${CONSTANT.WX.weiXinLogin}?code=${code}`
            Vue.http.post(url, null, { emulateJSON: true }).then(
                res => {
                    let data = res.data;
                    if (data.status == 200) {
                        let dataObj = data.data.bussData
                        localStorage.setItem("weixinId", dataObj.userId);
                        localStorage.setItem("openId", dataObj.openId);
                        let type = localStorage.getItem('userType');
                        if (type == 'normal') {
                            path = 'phone'
                        } else if (type == 'special') {
                            path = 'password?type=password'
                        } else if (type == 'store') {
                            path = 'password?type=store'
                        } else {
                            router.replace('./phone')
                        }
                        location.href = `${API}#` + path
                    } else {
                       alert('授权失败:'+data.msg||'')
                    }
                },
                res => {
                    console.log(res);
                }
            );
            return;
        }
        localStorage.setItem('rediect', 'ok')
        let rejectUrl = encodeURIComponent(`${API}${location.hash}${location.search}`)
        location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&&response_type=code&scope=snsapi_userinfo&redirect_uri=${rejectUrl}&state=STATE#wechat_redirect`
    } else if (webUrl.indexOf('from') > -1) {
        location.href = `${API}`
    }
    // next();
    // });
}

router.beforeEach((to, from, next) => {
    setTimeout(() => {
        window.scrollTo(0, 0)
    }, 100);
    console.log(to.meta)
    if (to.meta.checkWxjssdk && !sessionStorage.getItem('isload')) {
        next();
        common.isOnloadPage()
    } else {
        next();
    }
});


Vue.http.interceptors.push(function(request, next) {
    request.credentials = false;
    //拦截器
    next(res => {
        //401 登陆过期跳转
        console.log(res.status,'ssss.statusstatus')
        if (res.status == 200 && res.body.status == 401) {
            common.setCookie("sessionId", "");
            if (localStorage.getItem('userType') == 'normal') {
                router.replace('./phone')
            } else if (localStorage.getItem('userType') == 'special') {
                router.replace('./password')
            } else if (localStorage.getItem('userType') == 'store') {
                router.replace('./password?type=store')
            } else {
                console.log(176)
                    // console.log(location.href.indexOf('password?store=store')>-1)
                    // console.log(location.href.indexOf('password')>-1)
                if (location.href.includes('password?type=store')) {
                    router.replace('./password?type=store')
                } else if (location.href.includes('password')) {
                    router.replace('./password')
                } else {
                    router.replace('./phone')
                }
            }
            // location.href = `${API}#`+location.href.split('#')[1]
        }else if (res.status == 200 && res.body.status == 5024) {
            localStorage.setItem("weixinId", '');
            localStorage.setItem("openId", '');
            intercept()
        } else if (res.status == 500) {
            localStorage.setItem("weixinId", '');
            localStorage.setItem("openId", '');
            intercept()
        }
    });
});
if (!location.href.includes('localhost')) {
    intercept()
}
/* eslint-disable no-new */
new Vue({
    router,
    render: h => h(App)
}).$mount('#app-box')